package com.itheima.pinda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pinda.DTO.transportline.TransportLineTypeDto;
import com.itheima.pinda.common.CustomIdGenerator;
import com.itheima.pinda.common.utils.Constant;
import com.itheima.pinda.entity.PdTransportLineType;
import com.itheima.pinda.mapper.PdTransportLineTypeMapper;
import com.itheima.pinda.service.PdTransportLineTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class PdTransportLineTypeServiceImpl implements PdTransportLineTypeService {

    @Autowired
    private CustomIdGenerator idGenerator;
    @Autowired
    private PdTransportLineTypeMapper transportLineTypeMapper;

    @Override
    public TransportLineTypeDto insert(TransportLineTypeDto transportLineTypeDto) {
        PdTransportLineType transportLineType = new PdTransportLineType();
        BeanUtils.copyProperties(transportLineTypeDto,transportLineType);

        if (StringUtils.isEmpty(transportLineType.getId())) {
            // 新增
            transportLineType.setId(idGenerator.nextId(transportLineType) + "");
            transportLineTypeMapper.insert(transportLineType);
            transportLineTypeDto.setId(transportLineType.getId());
        }else {
            // 修改
            transportLineTypeMapper.updateById(transportLineType);
        }
        return transportLineTypeDto;
    }

    @Override
    public TransportLineTypeDto selectById(String id) {
        PdTransportLineType pdTransportLineType = transportLineTypeMapper.selectById(id);
        TransportLineTypeDto transportLineTypeDto = new TransportLineTypeDto();
        BeanUtils.copyProperties(pdTransportLineType,transportLineTypeDto);
        return transportLineTypeDto;
    }

    @Override
    public IPage selectByPage(Integer pageNum, Integer pageSize, String typeNumber, String name, Integer agencyType) {
        Page<PdTransportLineType> page = new Page<>(pageNum, pageSize);
        Page<PdTransportLineType> selectPage = transportLineTypeMapper.selectPage(page,
                new LambdaQueryWrapper<PdTransportLineType>()
                        .eq(!StringUtils.isEmpty(typeNumber), PdTransportLineType::getTypeNumber, typeNumber)
                        .eq(!StringUtils.isEmpty(name), PdTransportLineType::getName, name)
                        .eq(!StringUtils.isEmpty(agencyType), PdTransportLineType::getStartAgencyType, agencyType)
                        .eq(!StringUtils.isEmpty(agencyType), PdTransportLineType::getEndAgencyType, agencyType));

        ArrayList<TransportLineTypeDto> transportLineTypeDtos = new ArrayList<>();
        List<PdTransportLineType> records = selectPage.getRecords();
        for (PdTransportLineType pdTransportLineType : records) {
            TransportLineTypeDto transportLineTypeDto = new TransportLineTypeDto();
            BeanUtils.copyProperties(pdTransportLineType,transportLineTypeDto);
            transportLineTypeDtos.add(transportLineTypeDto);
        }

        return new Page<TransportLineTypeDto>().setRecords(transportLineTypeDtos);
    }

    @Override
    public void deleteById(List<String> ids) {
        transportLineTypeMapper.update(null,
                new LambdaUpdateWrapper<PdTransportLineType>()
                        .set(PdTransportLineType::getStatus, Constant.DATA_DISABLE_STATUS)
                        .in(PdTransportLineType::getId,ids));
    }
}
